<?php
namespace App\Repositories;

use App\Models\Simulate;

class PracticeRepository extends BaseRepository
{
    public function __construct(
        Simulate $simulate
    )
    {
        $this->model = $simulate;
    }


    public function index($data, $extra = '', $size = '')
    {
         $chapter_list = $this->model->where([['mid', $data['zid']]])->limit(1)->offset($data['page']-1)->get()->toArray();

        $count =  $this->model->where([['mid', $data['zid']]])->count();

        if($chapter_list){

            foreach($chapter_list as $k => $chapter){
                $chapter_list[$k]['answer'] = explode('||', $chapter['answer']);
                $chapter_list[$k]['titleUrl'] = $chapter_list[$k]['titleUrl'] ? 'http://www.exc360.com'.$chapter_list[$k]['titleUrl'] : $chapter_list[$k]['titleUrl'];
            }

        }
        return ['list'=>$chapter_list,'count'=>$count];

    }

    //获取驾校
    public function getSimulate($km)
    {
        //如果是科目一，练习试题比例为  判断题40 单选题60
        if($km == 1){
            $tm_judge = 40;
            $tm_single = 60;
            $tm_multiple = 0;

        //如果是科目四，练习题比例为   判断22  单选22  多选6
        }elseif($km == 2){
            $tm_judge = 22;
            $tm_single = 22;
            $tm_multiple = 6;
        }else{
            return [];
        }

        //随机获取单选题
        $single = $this->getSimulateID([['pid', $km], ['type', '1']], $tm_single);

        //随机获取多选题
        $multiple = $this->getSimulateID([['pid', $km], ['type', '2']], $tm_multiple);

        //随机获取判断题
        $judge = $this->getSimulateID([['pid', $km], ['type', '3']], $tm_judge);

        $simulate_data = array_merge($single, $multiple, $judge);
        //打乱题目
        shuffle($simulate_data);

        $data['question'] = $this->model->find($simulate_data[0]['id']);
        $data['question']['titleUrl'] = $data['question']['titleUrl'] ? 'http://www.exc360.com'.$data['question']['titleUrl'] : $data['question']['titleUrl'];

        $data['question']['answer'] = explode('||', $data['question']['answer']);
        $data['simulate'] = $simulate_data;

        return $data;

    }


    //根据条件获取题目ID
    public function getSimulateID($where, $limit){

        $where[] = ['stauts', '1'];
        $simulate_ids =  $this->model->select('id')->where($where)
                                            ->limit($limit)->inRandomOrder()
                                            ->get()->toArray();

        $data = [];
        if($simulate_ids){
            return $simulate_ids;
        }
        return $data;
    }


    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

    }


    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }


    public function learnList($data, $order_type)
    {

        $chapter_list = $this->model->where([['pid', $data['km']]])->limit(1)->offset($data['page']-1);

        if($order_type == 1){
            $chapter_list = $chapter_list->orderBy('id')->get()->toArray();
        }else{
            $chapter_list = $chapter_list->inRandomOrder()->get()->toArray();
        }

        $count =  $this->model->where([['pid', $data['km']]])->count();

        if($chapter_list){

            foreach($chapter_list as $k => $chapter){
                $chapter_list[$k]['answer'] = explode('||', $chapter['answer']);
                $chapter_list[$k]['titleUrl'] = $chapter_list[$k]['titleUrl'] ? 'http://www.exc360.com'.$chapter_list[$k]['titleUrl'] : $chapter_list[$k]['titleUrl'];
            }

        }
        return ['list'=>$chapter_list,'count'=>$count];

    }
}